<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>入库管理</title>
<meta name="decorator" content="default" />
<style type="text/css">
#inventoryTypeForm label {
	display: block;
}
</style>
<script type="text/javascript">
	global.bossPassword = "${fns:getStore().bossPassword}";
	$(document).ready(function() {
		$("#contentTable tbody tr").click(function() {
			$("#contentTable tbody tr").removeClass("select_active");
			$(this).addClass("select_active");
		});
	});
	function page(n, s) {
		$("#pageNo").val(n);
		$("#pageSize").val(s);
		$("#searchForm").submit();
		return false;
	}
	
	var redCheck = {
			close : function() {
				$("div#dialog-payCheck").popup('close');
				$("div#dialog-payCheck #error").hide();
				$("div#dialog-payCheck #paycode").val("");
				$("div#dialog-payCheck h3#title").html("");
				$("div#dialog-payCheck h3#content").html("");
				$("div#dialog-payCheck #error").text("");
			},
			checkCode : function(content,inId) {
				$("div#dialog-payCheck h3#title").html("冲红校验");
				//update content
				$("div#dialog-payCheck h3#content").html(content);
				//bind change event
				$("div#dialog-payCheck #paycode").unbind().bind("change",
						function() {
							$("div#dialog-payCheck #error").hide();
						});
				//bind check click check
				$("div#dialog-payCheck #check")
						.unbind()
						.bind(
								"click",
								function() {
									var inputCode = $(
											"div#dialog-payCheck #paycode").val();
									if (inputCode == null) {
										$("div#dialog-payCheck #error").text(
												"验证码错误，请核对输入是否正确");
										$("div#dialog-payCheck #error").show();
									} else {
										jQuery
												.ajax({
													type : "post",
													url : "${ctx}/inventory/in/checkRedVerifyCode",
													data : {
														verifyCode : inputCode,
													},
													success : function(data) {
														if (data) {
															var dataJson = JSON.parse(data.toLocaleString());
															if (dataJson[0] == 'true') {
																redCheck.close();
																red(inId);
															} else {
																$(
																		"div#dialog-payCheck #error")
																		.text(
																				"验证码错误，请核对输入是否正确");
																$(
																		"div#dialog-payCheck #error")
																		.show();
															}
														} else {
															$(
																	"div#dialog-payCheck #error")
																	.text(
																			"验证码错误，请核对输入是否正确");
															$(
																	"div#dialog-payCheck #error")
																	.show();
														}
													}
												});
									}
								});

				//bind resend click check
				$("div#dialog-payCheck #resend").unbind().bind("click", function() {
					 resend();
				});
				//show dialog
				$("#dialog-payCheck").popup("open");
			}
		};
	
	    function doRedCheck(inID){
	    	resend();
	    	redCheck.checkCode("请输入冲红校验码, 该校验码默认发送至店长手机", inID);
	    	//bossLoginDeletePopup('您将冲红此进货单，确认操作是否正确?', '${ctx}/inventory/in/red?id=${in.id}')
	    }
		
	    function red(inId){
	    	jQuery.ajax({
				type : "get",
				url : "${ctx}/inventory/in/red?id=" + inId,
				success : function(data) {
					var dataJson = $.parseJSON(data);
					if (dataJson.result == 'true'){
						showTip("冲红成功！");
						window.location.href = "${ctx}/inventory/in/";
					}
					if (dataJson.result == 'false'){
						var msg = "当前库存数量不够本次冲红的产品如下：<br/>";
						var arr = dataJson.msg.split(";");
						if (arr) {
							for (var i = 0, len = arr.length; i < len; i++) {
								msg += arr[i] + "<br/>";
							}
							global.alert(msg);
						} else {
							showTip("冲红失败！");
						}
					}
				}
			});
	    }
		function resend(){
			jQuery.ajax({
				type : "post",
				url : "${ctx}/inventory/in/resendRedVerifyCode",
				success : function(data) {
					if (data) {
						var dataJson = eval(data);
						if (dataJson[0]) {
							showTip("下发成功");
						}
					} else {
						showTip("下发失败");
					}
				}
			});
		}
	
</script>
</head>
<body>
	<div class="container">
		<div class="icon-buttons">
			<a data-role="button" data-inline="true" data-icon="back"
				data-iconpos="notext" href="${ctx}">返回</a>
		</div>
		<form:form id="searchForm" modelAttribute="inQuery"
			action="${ctx}/inventory/in/query" method="post" class="form-input">
			<input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}" />
			<input id="pageSize" name="pageSize" type="hidden"
				value="${page.pageSize}" />
			<label>开始日期：</label>
			<form:input type="date" id="startDate" name="startDate"
				path="startDate" />
			<label>结束日期：</label>
			<form:input type="date" id="endDate" name="endDate" path="endDate" />
			<input id="btnSubmit" data-inline="true" type="submit" value="查询进货单" />

		</form:form>
		<a href="#inventoryTypeSelection" data-rel="popup" data-inline="true"
			data-position-to="window" data-transition="pop" data-role="button">新增进货单</a>

		<tags:message content="${message}" />
		<table id="contentTable" data-role="table"
			class="ui-responsive table-stroke">
			<thead>
				<tr>
					<th colspan="5" style="text-align:center;">进货入库单</th>
				</tr>
				<tr>
					<th>入库日期</th>
					<th>操作人</th>
					<th>产品渠道</th>
					<th>总入库数量</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody>
				<c:if test="${empty inQuery.storeInventoryIns}">
					<tr>
						<td colspan="5">无相应数据</td>
					</tr>
				</c:if>
				<c:forEach items="${inQuery.storeInventoryIns}" var="in">
					<tr>
						<td>${fns:formatDate(in.inDate, 'yyyy-MM-dd HH:mm:ss')}</td>
						<td>${in.employee.name}</td>
						<td><c:choose>
								<c:when test="${in.inProductChannel==0}">系统产品</c:when>
								<c:when test="${in.inProductChannel==1}">店面自营产品</c:when>
							</c:choose></td>
						<td>${in.amount}</td>
						<td><a data-role="button" data-inline="true"
							href="${ctx}/inventory/in/detail?id=${in.id}">查看明细</a>&nbsp;&nbsp;
							<c:if test="${ in.status==0}">
								<button data-inline="true" onclick="return doRedCheck(${in.id})">冲红单据</button>
							</c:if> <c:if test="${in.status==1}">
								<button data-inline="true" disabled="disabled">已被冲红</button>
							</c:if>&nbsp;&nbsp;</td>
					</tr>
				</c:forEach>
			</tbody>
		</table>
		<div class="pagination">${page}</div>
	</div>

	<div data-role="popup" id="inventoryTypeSelection"
		data-overlay-theme="a" data-theme="c" data-dismissible="false"
		class="ui-corner-all">

		<div data-role="header" data-theme="a" class="ui-corner-top">
			<h1>温馨提示</h1>
		</div>
		<div data-role="content" style="width:20em; height: 10em"
			data-theme="d" class="ui-corner-bottom ui-content">
			<form id="inventoryTypeForm" action="${ctx}/inventory/in/create"
				method="post">
				<h3 class="ui-title">请选择入库产品渠道:</h3>
				<input type="radio" name="inventoryType" id="SYSTEM_OP"
					value="system" checked="checked" /> <label for="SYSTEM_OP">系统产品入库</label>
				<input type="radio" name="inventoryType" id="STORE_SELF_OP"
					value="storeSelf" /> <label for="STORE_SELF_OP">店面自营产品入库</label>
			</form>
		</div>
		<div data-theme="a" data-role="footer">
			<fieldset data-role="controlgroup" data-type="horizontal"
				data-inline="true">
				<button data-inline="true"
					onclick="$('#inventoryTypeForm').submit()">确定</button>
				<button type="reset" data-inline="true"
					onclick="$('#inventoryTypeSelection').popup('close');"
					data-theme="a">取消</button>
			</fieldset>
		</div>

	</div>
</body>
</html>
